package com.gogowan.agroenterprise.storage;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.base.TitleActivity;
import com.gogowan.agroenterprise.contorl.WeakRefAsyncTask;
import com.gogowan.agroenterprise.contorl.WeakRefAsyncTask.AsyncLoadListener;
import com.gogowan.agroenterprise.contorl.WeakRefAsyncTask.InterActionModel;
import com.gogowan.agroenterprise.model.BoxCode;
import com.gogowan.agroenterprise.model.HistoryRecordsRequest;
import com.gogowan.agroenterprise.model.LoginResultInfo;
import com.gogowan.agroenterprise.model.Message;
import com.gogowan.agroenterprise.model.RecordDetailResult;
import com.gogowan.agroenterprise.utils.PalUtils;
import com.gogowan.agroenterprise.utils.Utils;
import com.gogowan.agroenterprise.widget.adapter.HistoryDetailsAdapter;
import com.gogowan.agroenterprise.widget.xlistview.LoadingLayout;

/**
 * @Description:历史记录详情
 * @author mdp
 * @date 2017年1月6日 上午9:18:06
 */
public class HistoryDetailsActivity extends TitleActivity implements AsyncLoadListener {

	/** 1 box 2 gift 3duo */
	private int flag;
	private LoginResultInfo loginResult;
	private BoxCode boxCode;
	private ArrayList<BoxCode> boxCodeList = new ArrayList<>();
	private HistoryDetailsAdapter historyDetailsAdapter;
	private WeakRefAsyncTask weakRefAsyncTask = new WeakRefAsyncTask(this);
	private LoadingLayout loadingLayout;
	private static final int REFRESH_RQT = 1;
	// s-handler
	private static final int RELOAD_HDL = 1;
	private static final int NULL_HDL = 2;
	private static final int REFRESH_HDL = 3;

	@Override
	protected int setContentViewID() {
		return R.layout.activity_history_details;
	}

	@Override
	protected boolean initData(Intent intent, Bundle savedInstanceState) {
		loginResult = PalUtils.getLoginResultInfo(getApplicationContext());
		flag = intent.getIntExtra(PalUtils.PACK_TYPE_ITT, 0);
		boxCode = (BoxCode) intent.getSerializableExtra(PalUtils.PACK_MODEL_ITT);
		return flag != 0 && loginResult != null && boxCode != null;
	}

	@Override
	protected void initView() {
		switch (flag) {
		case 1:
			initTitle("装箱信息详情", false);
			((TextView) findViewById(R.id.history_details_xmnr_tv0)).setText("箱码内容:");
			break;
		case 2:
			initTitle("礼盒信息详情", false);
			((TextView) findViewById(R.id.history_details_xmnr_tv0)).setText("礼盒码内容:");
			break;
		case 3:
			initTitle("装垛信息详情", false);
			((TextView) findViewById(R.id.history_details_xmnr_tv0)).setText("垛码内容:");
			findViewById(R.id.cpmc).setVisibility(View.GONE);
			break;
		}
		
		((TextView) findViewById(R.id.history_details_xmnr_tv)).setText(boxCode.getCode());
		((TextView) findViewById(R.id.history_details_cpmc_tv)).setText(boxCode.getMaterialName());
		historyDetailsAdapter = new HistoryDetailsAdapter(this, boxCodeList, R.layout.layout_unpack_item);
		ListView listView = (ListView) findViewById(R.id.history_details_lv);
		listView.setAdapter(historyDetailsAdapter);
		// 加载页控件
		loadingLayout = (LoadingLayout) findViewById(R.id.history_details_loadingLayout);
		loadingLayout.setReplacedView(listView);
		loadingLayout.showLoading();
		requestList();
	}

	@Override
	protected void onClicked(View v) {

	}

	@Override
	public void handleMessage(TitleActivity activity, android.os.Message msg) {
		switch (msg.what) {
		case RELOAD_HDL:
			loadingLayout.showReload(new LoadingLayout.OnReloadClickListener() {
				@Override
				public void onClick() {
					requestList();
				}
			});
			break;
		case NULL_HDL:
			loadingLayout.showNull(new LoadingLayout.OnNullClickListener() {
				@Override
				public void onClick() {
					requestList();
				}
			});
			break;
		case REFRESH_HDL:
			loadingLayout.hide();
			historyDetailsAdapter.notifyDataSetChanged();
			break;
		default:
			break;
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		weakRefAsyncTask.cancel();
	}

	@Override
	public void onResult(AsyncLoadListener asyncLoadListener, String response, int actionId) {
		hideProgressDialog();
		if (isAlreadyDestroyed()) {
			return;
		}
		if (PalUtils.isEmpty(response)) {
			PalUtils.showShortToast(getApplicationContext(), getString(R.string.server_error));
			getWeakRefHandler().sendEmptyMessage(RELOAD_HDL);
		} else {
			switch (actionId) {
			// 提交
			case REFRESH_RQT:
				RecordDetailResult recordDetailResult = (RecordDetailResult) PalUtils.getPostResult(response,
						RecordDetailResult.class);
				if (recordDetailResult == null) {
					PalUtils.showShortToast(getApplicationContext(), getString(R.string.analyse_error));
					getWeakRefHandler().sendEmptyMessage(RELOAD_HDL);
				} else if (recordDetailResult.getResult() > 0) {
					if (PalUtils.isEmpty(recordDetailResult.getList())) {
						getWeakRefHandler().sendEmptyMessage(NULL_HDL);
					} else {
						boxCodeList.clear();
						boxCodeList.addAll(recordDetailResult.getList());
						getWeakRefHandler().sendEmptyMessage(REFRESH_HDL);
					}
				} else {
					PalUtils.showShortToast(getApplicationContext(), recordDetailResult.getResultinfo());
					getWeakRefHandler().sendEmptyMessage(NULL_HDL);
				}
				break;
			}
		}
	}

	/**
	 * 提交
	 */
	private void requestList() {
		HistoryRecordsRequest historyRecordsRequest = new HistoryRecordsRequest();
		//historyRecordsRequest.setEnterpriseId(loginResult.getEnterpriseinfo().getEnterpriseid());
		//historyRecordsRequest.setUserId(loginResult.getUserinfo().getPersonid());
		historyRecordsRequest.setCode(boxCode.getCode());
		historyRecordsRequest.setType(flag);
		String json = Utils.toJSON(historyRecordsRequest);
		Message message = new Message(PalUtils.MsgType.RecordsDetail, PalUtils.ENCRYPT, json);
		InterActionModel interActionModel = new InterActionModel();
		interActionModel.setIndex(REFRESH_RQT);
		interActionModel.setUrl(getString(R.string.first_url) + getString(R.string.first_url_page));
		interActionModel.setMessage(message);
		weakRefAsyncTask.execute(interActionModel);
	}
}
